package com.cogent.expression.impl;

import java.util.HashMap;

import com.cogent.business.cache.UserCacheManager;
import com.cogent.business.domain.UserDomain;
import com.cogent.business.helper.DistrictHelper;
import com.cogent.common.date.JodaDateUtil;
import com.cogent.common.util.ConstantUtil;
import com.cogent.common.util.MapUtil;
import com.cogent.expression.IExpression;

public class SystemExpressionImpl implements IExpression {
	public String getExpressionValue(HashMap formMap, String expression) {
		String[] curtemps = expression.split("\\|");
		if(curtemps[0].equals("SYSTEM.DATE")) {
			return JodaDateUtil.getCurDate("yyyy-MM-dd");
		} else if(curtemps[0].equals("SYSTEM.USER_REALNAME")) {
			Object obj = UserCacheManager.get(MapUtil.getStringValue(formMap, ConstantUtil.GLOBALS_USER_ID_KEY));
			if(obj != null) {
				UserDomain userDomain = (UserDomain)obj;
				return userDomain.getName();
			}
		} else if(curtemps[0].equals("SYSTEM.USER_ID")) {
			Object obj = UserCacheManager.get(MapUtil.getStringValue(formMap, ConstantUtil.GLOBALS_USER_ID_KEY));
			if(obj != null) {
				UserDomain userDomain = (UserDomain)obj;
				return userDomain.getId();
			}
		} else if(curtemps[0].equals("SYSTEM.DEFALUTVALUE")) {
			return curtemps[1];
		} else if(curtemps[0].equals("SYSTEM.DATETIME")) {
			return JodaDateUtil.getCurDate("yyyy-MM-dd HH:mm:ss");
		}
		
		return "";
	}
	
	public void getFormValue(HashMap formMap, String key, String expression) {
		String val = MapUtil.getStringValue(formMap, key);
		if(expression.equals("TO_UPPER"))
			val = val.toUpperCase();
		else if(expression.equals("TO_LOWWER"))
			val = val.toLowerCase();		
		
		formMap.put(key, val);
	}
}
